package org.studiox.graph.janusgraph.common.util;

import org.apache.commons.lang.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtil {

  public static Long getCurrentTimestamp() {
    return System.currentTimeMillis();
  }

  public static Date getCurrentDate() {
    return new Date();
  }

  public static Date getDate(long timestamp) {
    return new Date(timestamp);
  }

  public static long getTimestamp(Date date) {
    return date.getTime();
  }

  public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";

  public static final String DEFAULT_DAY_FORMAT = "yyyy-MM-dd";

  public static String formatDate(String format, Date date) {
    if (StringUtils.isBlank(format) || null == date) {
      return null;
    }
    SimpleDateFormat sdf = new SimpleDateFormat(format);
    return sdf.format(date);
  }

  public static Date parseDate(String format, String dateStr) throws Exception {
    if (StringUtils.isBlank(format) || StringUtils.isBlank(dateStr)) {
      return null;
    }
    SimpleDateFormat sdf = new SimpleDateFormat(format);
    try {
      return sdf.parse(dateStr);
    } catch (Throwable e) {
      throw new Exception(
          "parse date string='"
              + dateStr
              + "' by format='"
              + format
              + "' failed, caused by "
              + e.getMessage(),
          e);
    }
  }
}
